package com.sk.utility;

import java.util.ArrayList;
import android.content.res.TypedArray;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.sk.cartoonindia.R;
import com.sk.cartoonindia.ShowToonsActivity;

public class ImageAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	int screenWidth;
	int screenHeight;
	private ShowToonsActivity activity;
	private ArrayList<String> _arrayImageItem;
	public ImageSource imgSource;

	public ImageAdapter(ShowToonsActivity a, ArrayList<String> d) {
		_arrayImageItem = d;
		activity = a;
		Display display = a.getWindowManager().getDefaultDisplay();
		screenWidth = display.getWidth();
		screenHeight = display.getHeight();
		imgSource = new ImageSource(a.getApplicationContext(), screenWidth,
				screenHeight);
		TypedArray attr = activity
				.obtainStyledAttributes(R.styleable.HelloGallery);
		mGalleryItemBackground = attr.getResourceId(
				R.styleable.HelloGallery_android_galleryItemBackground, 0);
		attr.recycle();
	}

	public int getCount() {
		return _arrayImageItem.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ImageView imageView = new ImageView(activity);
		Log.i("ShowToonsActivity", "Getting image for index " + position);
		imageView.setLayoutParams(new Gallery.LayoutParams(
				Gallery.LayoutParams.FILL_PARENT,
				Gallery.LayoutParams.FILL_PARENT));
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setBackgroundResource(mGalleryItemBackground);
		imgSource.ShowImage(_arrayImageItem.get(position), imageView);
		return imageView;
	}
}